package com.wedu.modules.dss.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.wedu.common.utils.PageUtils;
import com.wedu.common.utils.R;
import com.wedu.modules.dss.dao.DssDeviceInformationDao;
import com.wedu.modules.dss.dao.DssOperationStatusDao;
import com.wedu.modules.dss.dao.DssRepairManageDao;
import com.wedu.modules.dss.dao.OperationStatusDao;
import com.wedu.modules.dss.entity.OperationStatusEntity;
import com.wedu.modules.dss.entity.dto.breakDownDTO;
import com.wedu.modules.dss.entity.dto.manageDTO;
import com.wedu.modules.dss.entity.DssDeviceInformationEntity;
import com.wedu.modules.dss.entity.DssOperationStatusEntity;
import com.wedu.modules.dss.entity.DssRepairManageEntity;
import com.wedu.modules.dss.entity.Vo.DeviceFaultStatVO;
import com.wedu.modules.dss.entity.dto.breakDownDTO;
import com.wedu.modules.dss.service.DssOperationStatusService;
import com.wedu.modules.dss.service.DssRepairManageService;
import com.wedu.modules.sys.dao.SysUserDao;
import com.wedu.modules.sys.entity.SysUserEntity;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Service("DssRepairManageService")
public class DssRepairManageServiceImpl extends ServiceImpl<DssRepairManageDao, DssRepairManageEntity> implements DssRepairManageService {
    @Autowired
    DssDeviceInformationDao dssDeviceInformationDao;
    @Autowired
    DssRepairManageService dssRepairManageService;
    @Autowired
    DssOperationStatusDao dssOperationStatusDao;
    @Autowired
    DssOperationStatusService dssOperationStatusService;
    @Autowired
    OperationStatusDao operationStatusDao;
    @Autowired
    DssRepairManageDao dssRepairManageDao;
    @Autowired
    private SysUserDao sysUserDao;


    @Override
    public PageUtils queryPage(Map<String, Object> params, Long userId) {
        MPJLambdaWrapper<DssOperationStatusEntity> wrapper01 = new MPJLambdaWrapper<>();
        wrapper01.eq(DssOperationStatusEntity::getLightStatus, -1)
                .isNull(DssOperationStatusEntity::getLightOver); //使用isNull方法
        List<DssOperationStatusEntity> statusEntityList = dssOperationStatusDao.selectList(wrapper01);
        //先在维修库中查找是否存在该id设备，如果存在，接着判断维修状态是否为正常，正常，则加入，反之，则不加入
        List<Long> redId=new ArrayList<>();
        for (DssOperationStatusEntity entity : statusEntityList) {
            redId.add(entity.getDeviceId());
        }
        //判断该id在故障库中的信息，以前的维修状态均为正常，才能加入
//        Map<String,List<DssRepairManageEntity>> Historymap=new HashMap<>();//以设备ID为key，故障信息作为value

        for (int i = 0; i < redId.size(); i++) {
            QueryWrapper<DssRepairManageEntity> wrapper=new QueryWrapper<>();
            wrapper.eq("device_id",redId.get(i))
                    .ne("repair_status",1);
            DssRepairManageEntity manageEntity = dssRepairManageDao.selectOne(wrapper);
            //    Historymap.put(redId.get(i), manageEntityList);

            if (manageEntity==null) {
                DssRepairManageEntity dssRepairManageEntity = new DssRepairManageEntity();
                dssRepairManageEntity.setDeviceId(redId.get(i).toString());
                dssRepairManageService.save(dssRepairManageEntity);
            }
        }

//        String keyWords = (String) params.get("keyWords");//故障关键字
        String deviceNumber = (String) params.get("deviceNumber");//设备编号
        String deviceName = (String) params.get("deviceName");
        //根据用户ID 判断当前用户是操作工人    还是维修人工
        SysUserEntity sysUserEntity = sysUserDao.queryByUserId(userId);
        Integer isOperator = sysUserEntity.getIsOperator();

        if (isOperator == 1) {
            //当前用户是操作工人，展示待维修数据
            MPJLambdaWrapper<DssDeviceInformationEntity> wrapper = new MPJLambdaWrapper<DssDeviceInformationEntity>()
                    .select(DssDeviceInformationEntity::getDeviceId, DssDeviceInformationEntity::getDeviceName,
                            DssDeviceInformationEntity::getDeviceModel,DssDeviceInformationEntity::getId)
                    .select(DssOperationStatusEntity::getLightStart, DssOperationStatusEntity::getLightOver,
                            DssOperationStatusEntity::getLightStatus)
                    .select(DssRepairManageEntity::getRepairStatus,DssRepairManageEntity::getBreakdownCode)
                    .leftJoin(DssOperationStatusEntity.class, DssOperationStatusEntity::getDeviceId, DssDeviceInformationEntity::getId)
                    .leftJoin(DssRepairManageEntity.class, on->on.eq(DssRepairManageEntity::getDeviceId, DssDeviceInformationEntity::getId)
                            .eq(DssRepairManageEntity::getRepairStatus, -1))
                    .like(StringUtils.isNotBlank(deviceNumber), DssDeviceInformationEntity::getDeviceId, deviceNumber)//根据设备编号模糊查询
                    .like(StringUtils.isNotBlank(deviceName),DssDeviceInformationEntity::getDeviceName,deviceName)
                    .eq(DssOperationStatusEntity::getLightStatus, -1)//红灯列表中的设备维修状态都是待维修的,所以将维修状态都默认设置为 -1
                    .isNull(DssOperationStatusEntity::getLightOver)
                    .eq(DssRepairManageEntity::getRepairStatus,-1);;//操作工人界面只展示待维修数据
            List<breakDownDTO> breakDownDTOList = dssDeviceInformationDao.selectJoinList(breakDownDTO.class, wrapper);
            IPage<breakDownDTO> page = dssDeviceInformationDao.selectJoinPage(new Page<>(1, 10), breakDownDTO.class, wrapper);
//            R r=R.ok();
//            r.put("page",page);
//            return r;
            return new PageUtils(page);
        } else if (isOperator == 2 || isOperator == -1) {
            //当前用户是维修工人，展示维修中数据
            MPJLambdaWrapper<DssDeviceInformationEntity> wrapper = new MPJLambdaWrapper<DssDeviceInformationEntity>()
                    .select(DssDeviceInformationEntity::getDeviceId, DssDeviceInformationEntity::getDeviceName,
                            DssDeviceInformationEntity::getDeviceModel,DssDeviceInformationEntity::getId)
                    .select(DssOperationStatusEntity::getLightStart, DssOperationStatusEntity::getLightOver,
                            DssOperationStatusEntity::getLightStatus)
                    .select(DssRepairManageEntity::getRepairStatus,DssRepairManageEntity::getBreakdownCode)
                    .leftJoin(DssOperationStatusEntity.class, DssOperationStatusEntity::getDeviceId, DssDeviceInformationEntity::getId)
                    .leftJoin(DssRepairManageEntity.class, DssRepairManageEntity::getDeviceId, DssDeviceInformationEntity::getId)
                    .like(StringUtils.isNotBlank(deviceNumber), DssDeviceInformationEntity::getDeviceId, deviceNumber)//模糊查询
                    .like(StringUtils.isNotBlank(deviceName),DssDeviceInformationEntity::getDeviceName,deviceName)
                    .eq(DssOperationStatusEntity::getLightStatus, -1)//红灯列表中的设备维修状态都是待维修的,所以将维修状态都默认设置为 0
                    .isNull(DssOperationStatusEntity::getLightOver)
                    .eq(DssRepairManageEntity::getRepairStatus,0);//维修工人界面展示维修中数据
            List<breakDownDTO> breakDownDTOList = dssDeviceInformationDao.selectJoinList(breakDownDTO.class, wrapper);
            IPage<breakDownDTO> page = dssDeviceInformationDao.selectJoinPage(new Page<>(1, 10), breakDownDTO.class, wrapper);

            MPJLambdaWrapper<DssDeviceInformationEntity> wrapper2 = new MPJLambdaWrapper<DssDeviceInformationEntity>()
                    .select(DssDeviceInformationEntity::getDeviceId, DssDeviceInformationEntity::getDeviceName,
                            DssDeviceInformationEntity::getDeviceModel,DssDeviceInformationEntity::getId)
                    .select(DssOperationStatusEntity::getLightStart, DssOperationStatusEntity::getLightOver,
                            DssOperationStatusEntity::getLightStatus)
                    .select(DssRepairManageEntity::getRepairStatus,DssRepairManageEntity::getBreakdownCode)
                    .leftJoin(DssOperationStatusEntity.class, DssOperationStatusEntity::getDeviceId, DssDeviceInformationEntity::getId)
                    .leftJoin(DssRepairManageEntity.class, DssRepairManageEntity::getDeviceId, DssDeviceInformationEntity::getId)
//                .like(StringUtils.isNotBlank(deviceNumber), DssDeviceInformationEntity::getDeviceId, deviceNumber)//模糊查询
                    .eq(DssOperationStatusEntity::getLightStatus, -1)//红灯列表中的设备维修状态都是待维修的,所以将维修状态都默认设置为 0
                    .isNull(DssOperationStatusEntity::getLightOver)
                    .eq(DssRepairManageEntity::getRepairStatus,0);//维修工人界面展示维修中数据据
//                .isNotNull(DssRepairManageEntity::getBreakdownCode);
            List<breakDownDTO> breakDownDTOList2 = dssDeviceInformationDao.selectJoinList(breakDownDTO.class, wrapper2);
            List<String> deviceNumberList=new ArrayList<>();
            for (int i = 0; i < breakDownDTOList2.size(); i++) {
                deviceNumberList.add(breakDownDTOList2.get(i).getDeviceId());
            }
//            R r=R.ok();
//            r.put("page",page);
//            r.put("deviceNumberList",deviceNumberList);
//            return r;
            return new PageUtils(page);
        }
        return null;
    }


    @Override
    public R apply(int[] ids) {
        //需要修改的对象
        List<DssRepairManageEntity> DRMlist=new ArrayList<>();
        for (int i = 0; i < ids.length; i++) {
            QueryWrapper<DssRepairManageEntity> wrapper=new QueryWrapper<>();
            wrapper.eq("device_id",ids[i])
                    .eq("repair_status",-1);
            DssRepairManageEntity dssRepairManageEntity = dssRepairManageDao.selectOne(wrapper);
//            System.out.println(dssRepairManageEntity);
//            dssRepairManageEntity.setRepairStatus(0);
            DRMlist.add(dssRepairManageEntity);
//            dssRepairManageService.updateById(dssRepairManageEntity);
        }
//        //传进来的设备编号在设备信息表中查找，然后得到主键id，再根据id到维修表中查找到该记录，修改状态
////        QueryWrapper<DssDeviceInformationEntity> wrapper1=new QueryWrapper<>();
////        wrapper1.in("device_id",Arrays.asList(ids));
////        List<DssDeviceInformationEntity> dssDeviceInformationEntityList = dssDeviceInformationDao.selectList(wrapper1);
//        // 用前端传进来的id数组，在DssRepairManage表中查找对应的数据,修改它的维修状态值，用saveOrUpdate方法更新到前端
//        QueryWrapper<DssRepairManageEntity> wrapper = new QueryWrapper<>();
//        wrapper.in("device_id", Arrays.asList(ids))
//                .eq("repair_status",-1);
//        List<DssRepairManageEntity> DRMlist = dssRepairManageDao.selectList(wrapper);

        // 修改维修状态为 0,并收集到List中
        List<DssRepairManageEntity> updateList = DRMlist.stream()
                .peek(entity -> entity.setRepairStatus(0))
                .collect(Collectors.toList());
        //批量修改实体类
        boolean isSuccess = dssRepairManageService.updateBatchById(updateList);

        return R.ok("成功更新" + updateList.size() + "条记录")
                .put("data", updateList)
                .put("successCount", updateList.size());
    }

    @Override
    public R saveMsg(DssRepairManageEntity dssRepairManageEntity,Long userId) {
        //故障维修记录（每条维修记录都要登记）
        //根据传进来的实体对象得到设备id
        String deviceId = dssRepairManageEntity.getDeviceId();
        QueryWrapper<DssDeviceInformationEntity> wrapper1=new QueryWrapper<>();
        wrapper1.eq("device_id",deviceId);
        DssDeviceInformationEntity dssDeviceInformationEntity = dssDeviceInformationDao.selectOne(wrapper1);
        QueryWrapper<DssRepairManageEntity> wrapper=new QueryWrapper<>();
        wrapper.eq("device_id",dssDeviceInformationEntity.getId())
                .eq("repair_status",0);
        DssRepairManageEntity dssRepairManageEntity1 = dssRepairManageDao.selectOne(wrapper);

//        DssRepairManageEntity dssRepairManageEntity1=new DssRepairManageEntity();
        dssRepairManageEntity1.setBreakdownMes(dssRepairManageEntity.getBreakdownMes());
        dssRepairManageEntity1.setBreakdownCode(dssRepairManageEntity.getBreakdownCode());
        dssRepairManageEntity1.setRepairPlan(dssRepairManageEntity.getRepairPlan());
        dssRepairManageEntity1.setUserId(userId);
        dssRepairManageEntity1.setRepairStarttime(new Date());//添加开始维修时间
        dssRepairManageService.updateById(dssRepairManageEntity1);
        return R.ok();
    }

    @Override
    public DssRepairManageEntity getByDeviceId(Long deviceId) {
        //根据故障设备id找到故障库的该条记录
        QueryWrapper<DssRepairManageEntity> wrapper=new QueryWrapper<>();
        wrapper.eq("device_id",deviceId)
                .eq("repair_status",0);
        DssRepairManageEntity dssRepairManageEntity = dssRepairManageDao.selectOne(wrapper);
        return dssRepairManageEntity;
    }

    @Override
    public R complete(int Id, Long userId) {
//        QueryWrapper<DssRepairManageEntity> wrapper=new QueryWrapper<>();
//        wrapper.eq("device_id",Id).eq("repair_status",0);
        QueryWrapper<DssDeviceInformationEntity> wrapper2=new QueryWrapper<>();
        wrapper2.eq("id",Id);
        DssDeviceInformationEntity dssDeviceInformationEntity = dssDeviceInformationDao.selectOne(wrapper2);

        QueryWrapper<DssRepairManageEntity> wrapper=new QueryWrapper<>();
        wrapper.eq("device_id",Id).eq("repair_status",0);
        DssRepairManageEntity dssRepairManageEntity = dssRepairManageDao.selectOne(wrapper);
        if(StringUtils.isEmpty(dssRepairManageEntity.getBreakdownCode())){
            R r=R.ok();
            return r.error(404,"请先诊断，再继续操作");
        }else {
            Date date = new Date();
            dssRepairManageEntity.setCreateBy(userId);//
            dssRepairManageEntity.setCreateTime(date);
            dssRepairManageEntity.setRepairEndtime(date);
            dssRepairManageEntity.setRepairStatus(1);
            dssRepairManageDao.updateById(dssRepairManageEntity);

            //把故障设备修好之后要在灯表中新增一条维修好的记录
            //先把红灯设备的红灯结束时间添加进去
            QueryWrapper<OperationStatusEntity> wrapper1 = new QueryWrapper<>();
            wrapper1.eq("device_id", Id)
                    .isNull("light_over");
            OperationStatusEntity operationStatusEntity = operationStatusDao.selectOne(wrapper1);
            operationStatusEntity.setLightEndTime(date);
            operationStatusDao.updateById(operationStatusEntity);

            OperationStatusEntity operationStatusEntity1 = new OperationStatusEntity();
            operationStatusEntity1.setDeviceId(dssDeviceInformationEntity.getId());
            operationStatusEntity1.setLightStatus(1);
            operationStatusEntity1.setLightStartTime(date);
            dssOperationStatusService.save(operationStatusEntity1);
            //变灯
//            OperationStatusEntity operationStatusEntity3 = new OperationStatusEntity();
//            QueryWrapper<OperationStatusEntity> queryWrapper = new QueryWrapper<OperationStatusEntity>()
//                    .eq("device_id", Id)
//                    .isNull("light_over");
//            OperationStatusEntity operationStatusEntity1 = operationStatusDao.selectOne(queryWrapper);
//            operationStatusEntity1.setLightEndTime(new Date());
//            operationStatusDao.updateById(operationStatusEntity1);
//            operationStatusEntity.setDeviceId(aLong);
//            operationStatusEntity.setLightStatus(1);
//            operationStatusEntity.setLightStartTime(new Date());
//            operationStatusDao.insert(operationStatusEntity);
            R r = R.ok();
            r.put("dssRepairManageEntity", dssRepairManageEntity);
            r.put("dssOperationStatusEntity", operationStatusEntity);
            r.put("dssOperationStatusEntity1", operationStatusEntity1);
            return r;
        }
    }

    @Override
    public PageUtils queryManagePage(Map<String, Object> params,Long userId) {
        String did = (String) params.get("Did");
        String keyWords = (String) params.get("keyWords");//故障关键字
        //根据用户ID 判断当前用户是操作工人    还是维修人工
        SysUserEntity sysUserEntity = sysUserDao.queryByUserId(userId);
        Integer isOperator = sysUserEntity.getIsOperator();
        //如果按照设备id查询故障记录，则先在信息表中找到设备，取id
        QueryWrapper<DssDeviceInformationEntity> wrapper2=new QueryWrapper<>();
        wrapper2.eq("device_id",did);
        DssDeviceInformationEntity dssDeviceInformationEntity = dssDeviceInformationDao.selectOne(wrapper2);
        if (isOperator==1) {
            //操作工人
            MPJLambdaWrapper<DssRepairManageEntity> wrapper = new MPJLambdaWrapper<DssRepairManageEntity>()
                    .select(DssRepairManageEntity::getId,DssRepairManageEntity::getBreakdownMes, DssRepairManageEntity::getBreakdownCode,
                            DssRepairManageEntity::getUserId, DssRepairManageEntity::getRepairPlan
                            , DssRepairManageEntity::getRepairStatus,DssRepairManageEntity::getCreateTime)
                    .select(SysUserEntity::getRealName, SysUserEntity::getMobile)
                    .select(DssDeviceInformationEntity::getDeviceId)
                    .leftJoin(SysUserEntity.class, SysUserEntity::getUserId, DssRepairManageEntity::getUserId)
                    .leftJoin(DssDeviceInformationEntity.class,DssDeviceInformationEntity::getId,DssRepairManageEntity::getDeviceId)
                    .like(StringUtils.isNotBlank(keyWords),DssRepairManageEntity::getBreakdownMes,keyWords)
//                    .like(StringUtils.isNotBlank(did),DssRepairManageEntity::getDeviceId,dssDeviceInformationEntity.getId())
                    .eq(DssRepairManageEntity::getRepairStatus, 1)
                    .orderByDesc(DssRepairManageEntity::getCreateTime);
            List<manageDTO> list = dssRepairManageDao.selectJoinList(manageDTO.class, wrapper);
            IPage<manageDTO> page = dssRepairManageDao.selectJoinPage(new Page<>(1, 10), manageDTO.class, wrapper);
            return new PageUtils(page);
        }else if (isOperator == 2 || isOperator == -1){
            //维修工人
            MPJLambdaWrapper<DssRepairManageEntity> wrapper = new MPJLambdaWrapper<DssRepairManageEntity>()
                    .select(DssRepairManageEntity::getId,DssRepairManageEntity::getBreakdownMes, DssRepairManageEntity::getBreakdownCode,
                            DssRepairManageEntity::getUserId, DssRepairManageEntity::getRepairPlan
                            , DssRepairManageEntity::getRepairStatus,DssRepairManageEntity::getCreateTime)
                    .select(SysUserEntity::getRealName, SysUserEntity::getMobile)
                    .select(DssDeviceInformationEntity::getDeviceId)
                    .selectAs(DssDeviceInformationEntity::getId,manageDTO::getInfoid)
                    .leftJoin(SysUserEntity.class, SysUserEntity::getUserId, DssRepairManageEntity::getUserId)
                    .leftJoin(DssDeviceInformationEntity.class,DssDeviceInformationEntity::getId,DssRepairManageEntity::getDeviceId)
//                    .like(StringUtils.isNotBlank(did),DssRepairManageEntity::getDeviceId,dssDeviceInformationEntity.getId())
                    .like(StringUtils.isNotBlank(keyWords),DssRepairManageEntity::getBreakdownMes,keyWords)
                    .eq(DssRepairManageEntity::getRepairStatus, 1)
                    .orderByDesc(DssRepairManageEntity::getCreateTime);
            List<manageDTO> list = dssRepairManageDao.selectJoinList(manageDTO.class, wrapper);
            IPage<manageDTO> page = dssRepairManageDao.selectJoinPage(new Page<>(1, 10), manageDTO.class, wrapper);
            return new PageUtils(page);
        }
        return null;
    }

    @Override
    public R updateMsg(DssRepairManageEntity dssRepairManageEntity, Long userId) {
        //根据传进来的实体对象，得到他的设备infoid，根据设备id到故障库中查找该记录，然后修改
        int id = dssRepairManageEntity.getId();
        String deviceId = dssRepairManageEntity.getDeviceId();
        QueryWrapper<DssDeviceInformationEntity> wrapper1=new QueryWrapper<>();
        wrapper1.eq("id",deviceId);
        DssDeviceInformationEntity dssDeviceInformationEntity = dssDeviceInformationDao.selectOne(wrapper1);
//        String Id = dssRepairManageEntity.getDeviceId();
//        Date createTime = (Date) params.get("createTime");
        QueryWrapper<DssRepairManageEntity> wrapper=new QueryWrapper<>();
        wrapper.eq("id",id).eq("device_id",dssDeviceInformationEntity.getId());
        DssRepairManageEntity manageEntity = dssRepairManageDao.selectOne(wrapper);
        R r=R.ok();

        manageEntity.setUpdateBy(userId);
        manageEntity.setUpdateTime(new Date());
        manageEntity.setDeviceId(dssDeviceInformationEntity.getId().toString());
        dssRepairManageService.updateById(manageEntity);
        r.put("manageEntity",manageEntity);

        return r;
    }


    @Override
    public R hasCodeNumber(Map<String, Object> params) {
        MPJLambdaWrapper<DssDeviceInformationEntity> wrapper = new MPJLambdaWrapper<DssDeviceInformationEntity>()
                .select(DssDeviceInformationEntity::getDeviceId, DssDeviceInformationEntity::getDeviceName,
                        DssDeviceInformationEntity::getDeviceModel,DssDeviceInformationEntity::getId)
                .select(DssOperationStatusEntity::getLightStart, DssOperationStatusEntity::getLightOver,
                        DssOperationStatusEntity::getLightStatus)
                .select(DssRepairManageEntity::getRepairStatus,DssRepairManageEntity::getBreakdownCode)
                .leftJoin(DssOperationStatusEntity.class, DssOperationStatusEntity::getDeviceId, DssDeviceInformationEntity::getId)
                .leftJoin(DssRepairManageEntity.class, DssRepairManageEntity::getDeviceId, DssDeviceInformationEntity::getId)
//                .like(StringUtils.isNotBlank(deviceNumber), DssDeviceInformationEntity::getDeviceId, deviceNumber)//模糊查询
                .eq(DssOperationStatusEntity::getLightStatus, -1)//红灯列表中的设备维修状态都是待维修的,所以将维修状态都默认设置为 0
                .isNull(DssOperationStatusEntity::getLightOver)
                .eq(DssRepairManageEntity::getRepairStatus,0)//维修工人界面展示维修中数据据
                .isNull(DssRepairManageEntity::getBreakdownCode);
        List<breakDownDTO> breakDownDTOList = dssDeviceInformationDao.selectJoinList(breakDownDTO.class, wrapper);
        int size = breakDownDTOList.size();
        R r=R.ok();
        IPage<breakDownDTO> page = dssDeviceInformationDao.selectJoinPage(new Page<>(1, 10), breakDownDTO.class, wrapper);
        r.put("size",size);
        r.put("page",page);
        return r;
    }

    @Override
    public R NoCodeNumber(Map<String, Object> params) {
        MPJLambdaWrapper<DssDeviceInformationEntity> wrapper = new MPJLambdaWrapper<DssDeviceInformationEntity>()
                .select(DssDeviceInformationEntity::getDeviceId, DssDeviceInformationEntity::getDeviceName,
                        DssDeviceInformationEntity::getDeviceModel,DssDeviceInformationEntity::getId)
                .select(DssOperationStatusEntity::getLightStart, DssOperationStatusEntity::getLightOver,
                        DssOperationStatusEntity::getLightStatus)
                .select(DssRepairManageEntity::getRepairStatus,DssRepairManageEntity::getBreakdownCode)
                .leftJoin(DssOperationStatusEntity.class, DssOperationStatusEntity::getDeviceId, DssDeviceInformationEntity::getId)
                .leftJoin(DssRepairManageEntity.class, DssRepairManageEntity::getDeviceId, DssDeviceInformationEntity::getId)
//                .like(StringUtils.isNotBlank(deviceNumber), DssDeviceInformationEntity::getDeviceId, deviceNumber)//模糊查询
                .eq(DssOperationStatusEntity::getLightStatus, -1)//红灯列表中的设备维修状态都是待维修的,所以将维修状态都默认设置为 0
                .isNull(DssOperationStatusEntity::getLightOver)
                .eq(DssRepairManageEntity::getRepairStatus,0)//维修工人界面展示维修中数据据
                .isNotNull(DssRepairManageEntity::getBreakdownCode);
        List<breakDownDTO> breakDownDTOList = dssDeviceInformationDao.selectJoinList(breakDownDTO.class, wrapper);
        int size = breakDownDTOList.size();
        R r=R.ok();
        IPage<breakDownDTO> page = dssDeviceInformationDao.selectJoinPage(new Page<>(1, 10), breakDownDTO.class, wrapper);
        r.put("size",size);
        r.put("page",page);
        return r;
    }

    @Override
    public R getFaultStatistics(String deviceId) {
        QueryWrapper<DssDeviceInformationEntity> wrapper=new QueryWrapper<>();
        wrapper.eq("device_id",deviceId);
        DssDeviceInformationEntity dssDeviceInformationEntity = dssDeviceInformationDao.selectOne(wrapper);
        Set<String> codeSet=new TreeSet<>();//故障码的类别
        List<Integer> numberList=new ArrayList<>();//每个故障码出现的次数
        List<DeviceFaultStatVO> deviceFaultStatVOList = dssRepairManageDao.selectFaultStatistics(dssDeviceInformationEntity.getId());//id设备问题出现次数
        for (DeviceFaultStatVO vo : deviceFaultStatVOList) {
            codeSet.add(vo.getBreakdownCode());
            numberList.add(vo.getCount());
        }
        R r=R.ok();
        r.put("list",deviceFaultStatVOList);
        r.put("codeSet",codeSet);
        r.put("numberList",numberList);
        return r;
    }


}

